/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Xceed.Silverlight.Data
{
  internal class CollapsedGroupCollection : ReadOnlyCollection<GroupCollection>
  {
    public CollapsedGroupCollection( IList<GroupCollection> groupCollections )
      : base( groupCollections )
    {
    }

    internal IEnumerable<Tuple<GroupState, DataPath>> IncludedGroups
    {
      get
      {
        return new GroupItemsEnumerable( this );
      }
    }

    private class GroupItemsEnumerable : IEnumerable<Tuple<GroupState, DataPath>>
    {
      public GroupItemsEnumerable(IEnumerable<GroupCollection> allCollections)
      {
        if( allCollections == null )
          throw new ArgumentNullException( "allCollections" );

        m_allCollections = allCollections;
      }

      #region IEnumerable<Tuple<GroupState,DataPath>> Members

      public IEnumerator<Tuple<GroupState, DataPath>> GetEnumerator()
      {
        return new GroupItemsEnumerator( m_allCollections.GetEnumerator() );
      }

      #endregion

      #region IEnumerable Members

      IEnumerator IEnumerable.GetEnumerator()
      {
        return this.GetEnumerator();
      }

      #endregion

      private IEnumerable<GroupCollection> m_allCollections;//null;
    }

    private class GroupItemsEnumerator : IEnumerator<Tuple<GroupState, DataPath>>
    {
      public GroupItemsEnumerator( IEnumerator<GroupCollection> allCollapsedLevels )
      {
        if(allCollapsedLevels == null)
          throw new ArgumentNullException("allCollapsedLevels");

        m_collectionEnumerator = allCollapsedLevels;
      }

      #region IEnumerator<Tuple<GroupState,DataPath>> Members

      public Tuple<GroupState, DataPath> Current
      {
        get
        {
          if(m_groupEnumerator == null)
            throw new InvalidOperationException( "Enumerator is out of bounds" );

          GroupState groupState = m_collectionEnumerator.Current.IsReversed ? GroupState.Expanded : GroupState.Collapsed;
          return new Tuple<GroupState, DataPath>( groupState, m_groupEnumerator.Current );
        }
      }

      #endregion

      #region IDisposable Members

      public void Dispose()
      {
        m_groupEnumerator = null;
        m_collectionEnumerator = null;
      }

      #endregion

      #region IEnumerator Members

      object IEnumerator.Current
      {
        get
        {
          return this.Current;
        }
      }

      public bool MoveNext()
      {
        if( m_groupEnumerator != null && m_groupEnumerator.MoveNext() )
          return true;

        if( !m_collectionEnumerator.MoveNext() )
          return false;

        m_groupEnumerator = m_collectionEnumerator.Current.InclusionList.GetEnumerator();

        return this.MoveNext();
      }

      public void Reset()
      {
        m_collectionEnumerator.Reset();
        m_groupEnumerator.Dispose();
        m_groupEnumerator = null;
      }

      #endregion

      private IEnumerator<DataPath> m_groupEnumerator; //null
      private IEnumerator<GroupCollection> m_collectionEnumerator; //null
    }

  }
}
